package com.selft;

import org.apache.http.HttpHost;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;

/**
 * Created by xi on 2016/12/7.
 */
public class EsClientFacotory {

    private static Client  client;

    public void ss(){};

    public static Client getInstance() throws Exception{
        // 9300 是我们java 连接的esserver 的端口。9200 是restapi 的接口
        Settings settings = Settings.builder()
                .put("client.transport.sniff", true)
                .put("cluster.name", "test")
               // .put("index.mapper.dynamic","false")
                .put("client.transport.ping_timeout", "1000s").build();


        //Settings settings =Settings.EMPTY;
        if(client == null){
            //PreBuiltTransportClient
            client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
                  //  .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host2"), 9300));
        }
        return client;
    }

    public static Client getRemoteInstance() throws Exception{
        // 9300 是我们java 连接的esserver 的端口。9200 是restapi 的接口
        Settings settings = Settings.builder()
                .put("client.transport.sniff", true)
                .put("cluster.name", "my-application")
                .put("client.transport.ping_timeout", "1000s").build();

        TransportClient client2 = null;
        //Settings settings =Settings.EMPTY;
        if(client == null){
            //PreBuiltTransportClient
            client2 = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.1.106"), 9300));
            //  .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host2"), 9300));
        }
        return client2;
    }

    public static Client getEsLogInstance() throws Exception{
        // 9300 是我们java 连接的esserver 的端口。9200 是restapi 的接口
        Settings settings = Settings.builder()
                .put("client.transport.sniff", true)
                .put("cluster.name", "elasticsearch-test")
                .put("client.transport.ping_timeout", "1000s").build();

        TransportClient client2 = null;
        //Settings settings =Settings.EMPTY;
        if(client == null){
            //PreBuiltTransportClient
            client2 = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.2.25"), 9300));
            //  .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host2"), 9300));
        }
        return client2;
    }


    public static RestClient getRestClient(){
        return    RestClient.builder(
                new HttpHost("localhost", 9200, "http")).build();
    }

    //添加3条消息
    public static RestHighLevelClient getRestHighLevelInstance() throws Exception{
        RestClient restClient = RestClient.builder(
                new HttpHost("localhost", 9200, "http"),
                new HttpHost("localhost", 9201, "http")).build();

        //high level
        RestHighLevelClient client = new RestHighLevelClient(restClient);
        return client;
    }
    public static void close(){
        if(client!=null){
            client.close();
        }
    }
}
